﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace SmartHelper
{
    /// <summary>
    /// 网页帮助类
    /// </summary>
    public class WebHelper
    {
        private static readonly object CurrentPageKey = new object();
        /// <summary>
        /// 弹框提示
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowClientMessage(string msg)
        {
            Page currentPage = GetCurrentPage();
            currentPage.ClientScript.RegisterStartupScript(currentPage.GetType(), "", "<script>alert('" + msg + "')</script>");
            
        }

        /// <summary>
        /// 弹框提示，并且跳转到指定页面
        /// </summary>
        /// <param name="msg">弹框信息</param>
        /// <param name="url">跳转页面</param>
        public static void AlertAndRedirect(string msg, string url)
        {
            HttpContext.Current.Response.Write($"<script>alert('{msg}');window.location.href ='{url}'</script>");
        }
        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="url"></param>
        /// <param name="endResponse"></param>
        public static void Redirect(string url, bool endResponse)
        {
            HttpContext.Current.Response.Redirect(url, endResponse);
        }

        /// <summary>
        /// 获取当前页面对象
        /// </summary>
        /// <returns></returns>
        public static Page GetCurrentPage()
        {
            return ((Page)HttpContext.Current.Items[CurrentPageKey]) ?? (HttpContext.Current.CurrentHandler as Page);
        }
        public static void AlertAndRefresh(string msg)
        {
            string js = string.Format("alert('{0}');window.location.href=window.location.href;", msg);
            RendScript(js);
        }
        private static void RendScript(string js)
        {
            string script = string.Format("<script type=\"text/javascript\" language=\"javascript\">{0}</script>", js);
            HttpContext.Current.Response.Write(script);
        }

    }
}